import type { RequiredDeep } from 'type-fest'

import { ajax } from '@/utils'

import type { components } from '../schema'
import type { IDeleteReq } from '../type'

/**
 * 菜单接口
 */
export const menuApi = {
  /**
   * 获取菜单列表
   */
  getMenuList(data: IGetMenuListReq): Promise<IGetMenuListRes> {
    return ajax.post('/system/menu/list', data)
  },
  /**
   * 获取菜单树
   */
  getMenuTree(): Promise<IGetMenuTreeRes> {
    return ajax.get('/system/menu/tree')
  },
  /**
   * 添加菜单
   */
  createMenu(data: ICreateMenuReq) {
    return ajax.post('/system/menu/create', data)
  },
  /**
   * 更新菜单
   */
  updateMenu(id: string, data: IUpdateMenuReq) {
    return ajax.put('/system/menu/update', data, { params: { id } })
  },
  /**
   * 删除菜单
   */
  deleteMenu(data: IDeleteReq) {
    return ajax.delete('/system/menu/delete', { data })
  }
}

export type IGetMenuListReq = components['schemas']['FindMenuListDTO']
export type IGetMenuListRes = RequiredDeep<
  components['schemas']['FindMenuListVO']['data']
>
export type IMenuListItem = IGetMenuListRes[0]
export type IGetMenuTreeRes = RequiredDeep<
  components['schemas']['GetMenuTreeVO']['data']
>
export type ICreateMenuReq = components['schemas']['CreateMenuDTO']
export type IUpdateMenuReq = components['schemas']['UpdateMenuDTO']
